package com.edoomath.api.web.controller.course;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.edoomath.api.servcice.CourseItemService;
import com.edoomath.api.servcice.CourseService;
import com.edoomath.api.servcice.UserCourseService;
import com.edoomath.api.servcice.UserService;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseItem;
import com.edoomath.entity.course.CourseVersion;
import com.edoomath.entity.user.User;
import com.edoomath.entity.user.UserWallet;
import com.edoomath.entity.usercourse.UserCourse;
import com.edoomath.entity.usercourse.UserCourseAttribute;
import com.edoomath.entity.usercourse.UserCourseItem;
import com.edoomath.entity.usercourse.UserCourseStat;
import com.edoomath.entity.usercourse.UserCourseTask;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/user-courses")
public class UserCourseController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private UserCourseService userCourseService;
	
	@Autowired
	private CourseItemService courseItemService;
	
	/**
	 * 
	 * @param unionid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public HttpResult list(@RequestParam("unionid") String unionid) {
		
		HttpResult httpResult = new HttpResult();
		
		User user = userService.load(unionid);
    	if(user.isChildren() && StringUtils.isNotBlank(user.getMobile())) {
    		user = userService.getParent(user);
    		unionid = user.getUnionid();
    	}
		
		UserWallet wallet = userService.saveOrGetUserWallet(user);
		
		List<UserCourse> userCourses = userCourseService.listUserCourse(unionid);
		Map<String, UserCourse> userCoursesMap = CollectionUtils.constructMapValue(userCourses, "course.id");
		List<Course> courses = CollectionUtils.extractPropertyList(userCourses, "course");
		
		List<UserCourseStat> stats = userCourseService.listUserCourseStat(unionid);
		Map<String, UserCourseStat> statsMap = CollectionUtils.constructMapValue(stats, "userCourse.course.id");
		
		int sumTasks = 0;
		int sumFinishedTasks = 0;
		for(UserCourseStat stat : stats) {
			sumTasks = sumTasks + stat.getTotalTheme();
			sumFinishedTasks = sumFinishedTasks +  stat.getFinishedTask();
		}
		
		
		Map<String, Map<String, UserCourseAttribute>>  attrs = userCourseService.listUserCourseAttrs(userCourses);
		
		return httpResult.success()
						 .data("wallet", wallet)
				         .data("courses", courses)
				         .data("attrs", attrs)
				         .data("stats", statsMap)
				         .data("userCourses", userCoursesMap)
				         .data("sumTasks", sumTasks)
				         .data("sumFinishedTasks", sumFinishedTasks)
				         .message("获取用户课程成功");
	}
	
	/**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/tasks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult listMaterialTasks(@PathVariable(value = "id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        // load user course
        UserCourse userCourse = userCourseService.load(UserCourse.class, id);
        
        List<UserCourseAttribute> attributes = userCourseService.listUserCourseAttribute(userCourse);
        Map<String, UserCourseAttribute> attrs = CollectionUtils.constructMapValue(attributes, "name");
        
        List<UserCourseItem> userCourseItems = userCourseService.listUserCourseItem(userCourse);
        Map<String, UserCourseItem> userCourseItemsMap = CollectionUtils.constructMapValue(userCourseItems, "courseItem.name");
        
        Map<String, List<UserCourseTask>> tasks = userCourseService.listUserCourseTasks(userCourse);
        return httpResult.success()
        				 .data("tasks", tasks)
        				 .data("userCourseItems", userCourseItemsMap)
        				 .data("attributes", attrs)
                         .data("course", userCourse.getCourse());
    }
	
	/**
	 * 
	 * @param userCourseId
	 * @param unionid
	 * @return
	 */
	@RequestMapping(value = "/{id}/items", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public HttpResult listItem(@PathVariable("id") Long userCourseId,
							   @RequestParam("unionid") String unionid) {
		
		HttpResult httpResult = new HttpResult();
		
		UserCourse userCourse = userCourseService.load(UserCourse.class, userCourseId);
		
		CourseVersion courseVersion = courseService.loadVersion(userCourse.getCourse());
		
		List<CourseItem> courseItems = courseItemService.listCourseItem(userCourse.getCourse());
		
		List<UserCourseItem> userCourseItems = userCourseService.listUserCourseItem(userCourse);
		Map<String, UserCourseItem> userCourseItemMap = CollectionUtils.constructMapValue(userCourseItems, "courseItem.id");
		
		return httpResult.success()
						 .data("userCourse", userCourse)
						 .data("course", userCourse.getCourse())
						 .data("courseVersion", courseVersion)
		         		 .data("courseItems", courseItems)
		         		 .data("userCourseItems", userCourseItemMap)
		         		 .message("获取用户课程成功");
	}
	
}
